package com.yc.web.servlets;

import com.yc.bean.User;
import com.yc.bean.tb_user;
import com.yc.model.JsonModel;
import com.yc.utils.EncryptUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwei
 * @create 2024-05-15 13:31
 */
@WebServlet("/user.action")
public class UserServlet extends BaseServlet {
    protected void getAllUser(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
        JsonModel jm = new JsonModel();
        List<User> list = dbHelper.select(User.class, "select*from user");
        if (list.size() > 0 || list.isEmpty()) {
            jm.setCode(1);
            jm.setObj(list);
            System.out.println(list);
        } else {
            jm.setCode(0);
            jm.setError("数据获取失败");
        }
        super.writeJson(jm, resp);
    }
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchAlgorithmException, IOException {
        String username=req.getParameter("username");
        String pwd=EncryptUtils.encryptToMD5(EncryptUtils.encryptToMD5(req.getParameter("password")));
        String sql="select * from cust where name=? and pwd=?";
        List<Map<String,Object>>list=dbHelper.select(sql,username,pwd);
        Map<String,Object>map=new HashMap<>();
        if (list.isEmpty()){
            map.put("code",1);
            map.put("msg","用户名或者密码错误");
            super.writeObjectToJson(map,resp);
            return;
        }
        map.put("code",0);
        map.put("msg","登陆成功");
        super.writeObjectToJson(map,resp);
    }
}
